package com.sxbbc.plat.controller.jz;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.CCustomer;
import com.sxbbc.common.core.entity.jz.CCustomerAuthen;
import com.sxbbc.common.core.entity.jz.CDonateDetail;
import com.sxbbc.common.core.entity.jz.CMoneyDetail;
import com.sxbbc.common.core.entity.jz.CPointDetail;
import com.sxbbc.common.core.entity.jz.CShareDetail;
import com.sxbbc.common.core.enums.HandleTypeEnums;
import com.sxbbc.common.core.service.IAccountChangeService;
import com.sxbbc.common.core.service.IAddressService;
import com.sxbbc.common.core.service.ICCustomerAuthenService;
import com.sxbbc.common.core.service.ICCustomerService;
import com.sxbbc.common.core.service.ICDonateDetailService;
import com.sxbbc.common.core.service.ICMoneyDetailService;
import com.sxbbc.common.core.service.ICPointDetailService;
import com.sxbbc.common.core.service.ICShareDetailService;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.plat.common.CustomerMapping;
import com.sxbbc.plat.util.SystemLog;
import org.springframework.web.util.HtmlUtils;

/**
 * 会员账户/金额 控制层
 *
 * @author leehao
 * @date 2019-05-07 15:26
 */
@Controller
public class CustomerAccountController {

    @Autowired
    private ICMoneyDetailService moneyDetailsService;
    @Autowired
    private ICPointDetailService pointDetailsService;
    @Autowired
    private ICDonateDetailService donateDetailsService;
    @Autowired
    private ICShareDetailService shareDetailsService;
    @Autowired
    private IAccountChangeService accountChangeService;
    @Autowired
    private ICCustomerAuthenService customerAuthenService;
    @Autowired
    private IAddressService addressService;
    @Autowired
    private ICCustomerService customerService;

    private List<HandleTypeEnums> handleList = HandleTypeEnums.queryList();

    /**
     * 会员金额明细页面
     * @param page 分页条件
     * @param details 查询条件
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = CustomerMapping.SHOW_CUSTOMER_MONEY_LIST)
    public ModelAndView showCustomerMoneyList(BasePage<CMoneyDetail> page, CMoneyDetail details) {
        if (StringUtils.isNotBlank(details.getQueryTime())) {
            String[] times = details.getQueryTime().split("~");
            details.setStart(times[0].trim());
            details.setEnd(times[1].trim());
        }
        IPage<CMoneyDetail> pageInfo = moneyDetailsService.queryCustomerMoneyList(page, details);
        ModelAndView view = new ModelAndView(CustomerMapping.SHOW_CUSTOMER_MONEY_LIST);
        view.addObject("pageInfo", pageInfo);
        view.addObject("details", details);
        view.addObject("handleList", handleList);
        return view;
    }

    /**
     * 会员积分明细页面
     * @param page 分页条件
     * @param details 查询条件
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = CustomerMapping.SHOW_CUSTOMER_POINT_LIST)
    public ModelAndView showCustomerPointList(BasePage<CPointDetail> page, CPointDetail details){
        if (StringUtils.isNotBlank(details.getQueryTime())) {
            String[] times = details.getQueryTime().split("~");
            details.setStart(times[0].trim());
            details.setEnd(times[1].trim());
        }
        IPage<CPointDetail> pageInfo = pointDetailsService.queryCustomerPointList(page,details);
        ModelAndView view = new ModelAndView(CustomerMapping.SHOW_CUSTOMER_POINT_LIST);
        view.addObject("pageInfo",pageInfo);
        view.addObject("handleList",handleList);
        view.addObject("details",details);
        return view;
    }
    
    /**
     * 会员贡献值明细页面
     * @param page 分页条件
     * @param details 查询条件
     * @author xiaoky
     * @return ModelAndView
     */
    @RequestMapping(value = CustomerMapping.SHOW_CUSTOMER_DONATE_LIST)
    public ModelAndView showCustomerDonateList(BasePage<CDonateDetail> page, CDonateDetail details){
        if (StringUtils.isNotBlank(details.getQueryTime())) {
            String[] times = details.getQueryTime().split("~");
            details.setStart(times[0].trim());
            details.setEnd(times[1].trim());
        }
        IPage<CDonateDetail> pageInfo = donateDetailsService.queryCustomerDonateList(page,details);
        ModelAndView view = new ModelAndView(CustomerMapping.SHOW_CUSTOMER_DONATE_LIST);
        view.addObject("pageInfo",pageInfo);
        view.addObject("handleList",handleList);
        view.addObject("details",details);
        return view;
    }
    
    /**
     * 会员共享值明细页面
     * @param page 分页条件
     * @param details 查询条件
     * @author xiaoky
     * @return ModelAndView
     */
    @RequestMapping(value = CustomerMapping.SHOW_CUSTOMER_SHARE_LIST)
    public ModelAndView showCustomerShareList(BasePage<CShareDetail> page, CShareDetail details){
    	if (StringUtils.isNotBlank(details.getQueryTime())) {
    		String[] times = details.getQueryTime().split("~");
    		details.setStart(times[0].trim());
    		details.setEnd(times[1].trim());
    	}
    	IPage<CShareDetail> pageInfo = shareDetailsService.queryCustomerShareList(page,details);
    	ModelAndView view = new ModelAndView(CustomerMapping.SHOW_CUSTOMER_SHARE_LIST);
    	view.addObject("pageInfo",pageInfo);
    	view.addObject("handleList",handleList);
    	view.addObject("details",details);
    	return view;
    }
    
    /**
     * 操作用户金额页面
     * @param isout 0 扣除  1赠送
     * @param cid
     * @return ModelAndView
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.SHOW_CHANGE_MONEY)
    public ModelAndView showHandleMoney (Integer isout, Integer cid) {
    	ModelAndView view = new ModelAndView(CustomerMapping.SHOW_CHANGE_MONEY);
    	view.addObject("isout", isout);
    	view.addObject("cid", cid);
    	view.addObject("handleList",handleList);
    	return view;
    }
    
    /**
     * 操作用户账户金额
     * @param cid 用户id
     * @param isOut 0 扣除  1赠送
     * @param typeId 见HandleTypeEnums
     * @param cost 操作金额
     * @return 结果
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.HANDLE_CHANGE_MONEY)
    @ResponseBody
    public RestResponse handleChangeMoney (Integer cid, Integer isOut, Integer typeId, BigDecimal cost) {
    	CalcuUtils calcuUtils = CalcuUtils.getInstance();
    	String payCode = calcuUtils.getOrderCode("");
    	String remark = "系统赠送";
    	if (calcuUtils.isEquals(isOut, StaticUtils.PAY_OUT)) {
    		remark = "系统扣除";
    	}
    	if (cost == null || BigDecimal.ZERO.compareTo(cost)>=0) {
    		return GetRest.getFail("请输入正确的操作值");
    	}
    	String msg = accountChangeService.insertCMoneyDetail(cid, typeId, isOut, payCode, cost, remark, null);
    	if (!StringUtils.isEmpty(msg)) {
    		throw new ThrowJsonException(msg);
    	}
    	return GetRest.getSuccess("操作成功");
    }
    /**
     * 操作用户积分页面
     * @param isout 0 扣除  1赠送
     * @param cid
     * @return ModelAndView
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.SHOW_CHANGE_POINT)
    public ModelAndView showHandlePoint (Integer isout, Integer cid) {
    	ModelAndView view = new ModelAndView(CustomerMapping.SHOW_CHANGE_POINT);
    	view.addObject("isout", isout);
    	view.addObject("cid", cid);
    	view.addObject("handleList",handleList);
    	return view;
    }
    
    /**
     * 操作用户账户积分
     * @param cid 用户id
     * @param isOut 0 扣除  1赠送
     * @param typeId 见HandleTypeEnums
     * @param cost 操作金额
     * @return 结果
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.HANDLE_CHANGE_POINT)
    @ResponseBody
    public RestResponse handleChangePoint (Integer cid, Integer isOut, Integer typeId, BigDecimal cost) {
    	CalcuUtils calcuUtils = CalcuUtils.getInstance();
    	String payCode = calcuUtils.getOrderCode("");
    	String remark = "系统赠送";
    	if (calcuUtils.isEquals(isOut, StaticUtils.PAY_OUT)) {
    		remark = "系统扣除";
    	}
    	if (cost == null || BigDecimal.ZERO.compareTo(cost)>=0) {
    		return GetRest.getFail("请输入正确的操作值");
    	}
    	String msg = accountChangeService.insertCPointDetail(cid, typeId, isOut, payCode, cost, remark, null);
    	if (!StringUtils.isEmpty(msg)) {
    		throw new ThrowJsonException(msg);
    	}
    	return GetRest.getSuccess("操作成功");
    }
    /**
     * 操作用户共享值页面
     * @param isout 0 扣除  1赠送
     * @param cid
     * @return ModelAndView
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.SHOW_CHANGE_SHARE)
    public ModelAndView showHandleShare (Integer isout, Integer cid) {
    	ModelAndView view = new ModelAndView(CustomerMapping.SHOW_CHANGE_SHARE);
    	view.addObject("isout", isout);
    	view.addObject("cid", cid);
    	view.addObject("handleList",handleList);
    	return view;
    }
    

    
    /**
     * 操作用户共享值页面
     * @param isout 0 扣除  1赠送
     * @param cid
     * @return ModelAndView
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.SHOW_CHANGE_DONATE)
    public ModelAndView showHandleDonate (Integer isout, Integer cid) {
    	ModelAndView view = new ModelAndView(CustomerMapping.SHOW_CHANGE_DONATE);
    	view.addObject("isout", isout);
    	view.addObject("cid", cid);
    	view.addObject("handleList",handleList);
    	return view;
    }
    

    
    /**
     * 用户认证数据
     * @return ModelAndView
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.SHOW_CUSTOMER_AUTHEN)
    public ModelAndView showCustomerAuthen () {
    	ModelAndView view = new ModelAndView(CustomerMapping.SHOW_CUSTOMER_AUTHEN);
    	return view;
    }
    
    /**
     * 查询用户认证数据
     * @param page 分页
     * @param authen 查询数据
     * @return 分页数据
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.QUERY_CUSTOMER_AUTHEN)
    @ResponseBody
    public com.sxbbc.common.basics.rest.RestResponse queryCustomerAuthen (BasePage<CCustomerAuthen> page, CCustomerAuthen authen) {
    	IPage<CCustomerAuthen> pageInfo = customerAuthenService.queryPage(page, authen);
    	page.getRecords().forEach(x-> {
    		//真实姓名转义
			x.setRealName(HtmlUtils.htmlEscape(x.getRealName()));
    		CCustomer c = customerService.getMineParent(x.getCid());
    		if(c == null) {
    			x.setParentName("无推荐人");
    		}else {
    			if(CalcuUtils.getInstance().isEquals(c.getAuthen(), StaticUtils.STATUS_YES)) {
    				x.setParentName(c.getRealName());
    			}else {
    				x.setParentName(c.getPhone());
    			}
    		}
    	});
    	return RestFactory.success(pageInfo.getRecords(), pageInfo.getTotal());
    }
    
    /**
     * 身份认证详情页面
     * @param authenId 数据id
     * @return
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.SHOW_AUTHEN_DETAIL)
    public ModelAndView showAuthenDetail (Integer authenId) {
    	ModelAndView view = new ModelAndView(CustomerMapping.SHOW_AUTHEN_DETAIL);
    	CCustomerAuthen authen = customerAuthenService.getById(authenId);
    	if (authen != null) {
    		CCustomer customer = customerService.getById(authen.getCid());
    		if (customer != null) {
    			authen.setCNickName(customer.getNickName());
    			authen.setCPhone(customer.getPhone());
    		}
    		String addrName = addressService.getAddrName(authen.getProId(), authen.getCityId(), authen.getAreaId());
    		authen.setAddressDetail(addrName + " " + authen.getAddressDetail());
    	}
    	view.addObject("authen", authen);
    	return view;
    }
    
    /**
     * 身份认证状态审核
     * @param id 数据id
     * @param status 状态
     * @return 
     * @author xiaoky
     */
    @RequestMapping(value = CustomerMapping.EDIT_AUTHEN_APPLY_STATUS)
    @ResponseBody
    public RestResponse editAuthenApplyStatus (Integer id, Integer status, String rejectRemark) {
    	return customerAuthenService.editAuthenApplyStatus(id, status, rejectRemark);
    }
    
    /**
     * 
     * 用户资料编辑页面
     * @param authenId
     * @return
     * @author Wahyee
     */
    @RequestMapping(value = CustomerMapping.SHOW_AUTHEN_DETAIL_EDIT)
    public ModelAndView  showAuthenDetailEdit(Integer authenId) {
    	ModelAndView  view = new ModelAndView(CustomerMapping.SHOW_AUTHEN_DETAIL_EDIT);
    	CCustomerAuthen authen = customerAuthenService.getById(authenId);
    	if (authen != null) {
    		CCustomer customer = customerService.getById(authen.getCid());
    		if (customer != null) {
    			authen.setCNickName(customer.getNickName());
    			authen.setCPhone(customer.getPhone());
    		}
    		String addrName = addressService.getAddrName(authen.getProId(), authen.getCityId(), authen.getAreaId());
    		authen.setAddressDetail(addrName + " " + authen.getAddressDetail());
    	}
    	view.addObject("authen", authen);
    	return view;
    }
    
    /**
     * 
     * 编辑用户认证资料
     * @return
     * @author Wahyee
     */
    @RequestMapping(value = CustomerMapping.EDIT_AUTHEN_DETAIL)
    @ResponseBody
    @SystemLog(methods = "编辑用户资料", module = "用户管理-身份认证")
    public RestResponse  editAuthenDetail(CCustomerAuthen authen) {

    	if(customerAuthenService.updateById(authen)) {
    		return GetRest.getSuccess("保存成功");
    	}
    	return GetRest.getFail("保存失败");
    }
}
